package com.systop.asset.service.impl;

import com.systop.asset.entity.Inventory;
import com.systop.asset.entity.Material;
import com.systop.asset.entity.MaterialRepository;
import com.systop.asset.entity.vo.MaterialListVo;
import com.systop.asset.enums.HttpCodeEnum;
import com.systop.asset.exception.SystemException;
import com.systop.asset.mapper.InventoryMapper;
import com.systop.asset.mapper.MaterialMapper;
import com.systop.asset.mapper.MaterialRepositoryMapper;
import com.systop.asset.service.IMaterialService;
import com.systop.asset.utils.RandomCodeGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class MaterialServiceImpl implements IMaterialService {

    @Autowired
    MaterialMapper materialMapper;
    @Autowired
    private InventoryMapper inventoryMapper;
    /**
     * 新增耗材、新增库存
     * @param material
     * @return
     */
    @Override
    @Transactional //开启事务
    public int addMaterial(Material material) {
        //判断耗材是否已经存在
        //根据耗材名称查询数据
        List<Material> list =   materialMapper.selectMaterialByName(material.getName(),material.getWarehouseId());
        //创建库存对象
        Inventory inventory = new Inventory();
        if (list == null || list.size() == 0){ //耗材不存在，新增耗材、然后新增库存
            //新增耗材
            String HC_Code = RandomCodeGenerator.generateCustomCode("HC");
            System.out.println(HC_Code);
            material.setCode(HC_Code);
            material.setCreateTime(new Date());
            //新增库存
            materialMapper.insertMaterial(material);
            //新增库存
            inventory.setMaterialId(material.getMaterialId());
            inventory.setCategoryId(material.getCategoryId());
            inventory.setQuantity(material.getQuantity());
            inventory.setWarehouseId(material.getWarehouseId());
            inventory.setCreatedTime(new Date());
            inventory.setUpdatedTime(new Date());
            inventoryMapper.insertInventory(inventory);
        }else{ //耗材存在，更新库存
            //查询库存
            inventory = inventoryMapper.selectInventoryById(list.get(0).getInventoryId());
            //修改库存数量
            inventory.setQuantity(inventory.getQuantity()+material.getQuantity());
            //耗材存在，只更新库存
            inventoryMapper.updateInventory(inventory);
        }

        return 1;
    }

    @Override
    public int delMaterial(int id) {
        return materialMapper.deleteMaterial(id);
    }

    @Override
    @Transactional
    public int editorMaterial(Material material) {
        //更新耗材
        materialMapper.editorMaterial(material);
        //根据耗材id更新库存
        //创建库存对象
        Inventory inventory = new Inventory();
        inventory.setMaterialId(material.getMaterialId());
        inventory.setCategoryId(material.getCategoryId());
        inventory.setQuantity(material.getQuantity());
        inventory.setWarehouseId(material.getWarehouseId());
        inventory.setUpdatedTime(new Date());
        int updateResult = inventoryMapper.updateInventoryByMaterialId(inventory);
        //更新失败：该耗材未入库
        if (updateResult==0){
            //新增入库
            inventory.setCreatedTime(new Date());
            return inventoryMapper.insertInventory(inventory);
        }
        return updateResult;
    }

    @Override
    public List<MaterialListVo> materialList(MaterialListVo vo) {
        int page = 1;
        if (vo.getPage()!=null && vo.getPage()>1 ){
            page = vo.getPage();
        }
        int pageSize = 10;
        if (vo.getPageSize()!=null && vo.getPageSize()>0 ){
            pageSize = vo.getPageSize();
        }
        vo.setOffset((page-1)*pageSize);
        return materialMapper.selectMaterial(vo);
    }

    @Override
    public Material getMaterialById(Long id,Long wid) {
        return materialMapper.getMaterialById(id,wid);
    }

    /**
     * 快速入库：更新库存数量
     * @param material
     * @return
     */
    @Override
    public int quickWarehouseInbound(Material material) {
        Material selectMaterial = materialMapper.getMaterialById(material.getMaterialId(),material.getWarehouseId());
        //创建库存对象
        Inventory inventory = new Inventory();
        inventory.setId(selectMaterial.getInventoryId());
        inventory.setMaterialId(material.getMaterialId());
        inventory.setQuantity(material.getQuantity()+selectMaterial.getQuantity());
        inventory.setUpdatedTime(new Date());
        return inventoryMapper.updateInventory(inventory);
    }
}
